package com.five.bi.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;

import javax.annotation.PostConstruct;
import java.sql.SQLException;
import java.util.Map;

@Component
@Slf4j
public class MapUtil {
    private static MapUtil mapUtil;

    @PostConstruct
    public void init() {
        mapUtil = this;

    }
    //（Int）类型转换器
    public static Integer converterOfInt(Object arg) throws NullPointerException {
        Integer result = null;
        try {
            result = (Integer) arg;
        } catch (Exception e) {
        }
        try {
            result = Integer.parseInt((String) arg);
        } catch (Exception e) {
        }
        if (result == null) {
            throw new NullPointerException("类型转换失败");
        }
        return result;
    }

    //(String)类型转换器
    public static String converterOfString(Object arg) throws NullPointerException {
        String result = null;
        try {
            result = "" + (Integer) arg;
        } catch (Exception e) {
        }
        try {
            result = (String) arg;
        } catch (Exception e) {
        }
        if (result == null) {
            throw new NullPointerException("类型转换失败");
        }
        return result;
    }

    //其他类型转换器 当需要查询身份时 必须
    public static <T>  T converterOfOther(Map map,Class<T> clazz) throws SQLException {
        return  JSON.parseObject(JSON.toJSONString(map), clazz);
    }

}
